//
//  ViewController.m
//  muce
//
//  Created by  yunshao on 2018/2/7.
//  Copyright © 2018年  yunshao. All rights reserved.
//

#import "ViewController.h"
#import "pch.h"
#import "DetailViewController.h"
#import "LPRequestParser.h"

@interface ViewController ()<UITextFieldDelegate>{
    
}
@property(nonatomic,strong)UIView *backView;                //背景色
@property(nonatomic,strong)UIImageView * loginImageBackView;
@property(nonatomic,strong)UIButton * sureButton;           //确定按钮
@property(nonatomic,strong)UILabel * phoneLabel;            //电话号码和邀请码
@property(nonatomic,strong)UITextField * passwordTextFiled; //电话号码和邀请码输入
@property(nonatomic,strong)UIImageView *picImageView;       //图片
@property (nonatomic,strong)UILabel * conetLabel;
@property (nonatomic,strong)UILabel * wwwLabel;             //网址




@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addMainView];
    [self requData];
}
-(void)requData{
    [LPRequestParser getImageUrlBlock:^(id responseObject, NSError *error) {
        if (responseObject) {
            
        }
    }];    
}
-(void)addMainView{
    _loginImageBackView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, DSDevice_W, DSDevice_H)];
    [_loginImageBackView setImage: [UIImage imageNamed:@"login_bg"]];
    
    _loginImageBackView.userInteractionEnabled = YES;
    [self.view addSubview:_loginImageBackView];
    
    _picImageView = [[UIImageView alloc]initWithFrame:CGRectMake(DSFixedPX(100), DSFixedPX(100), DSDevice_W - DSFixedPX(200), DSDevice_W - DSFixedPX(200))];
    _picImageView.image = [UIImage imageNamed:@"iconMuce"];
//    [_picImageView sizeToFit];
//    _picImageView.center = CGPointMake(DSDevice_W / 2, DSFixedPX(70) + _picImageView.frame.size.height / 2);
    [_loginImageBackView addSubview:_picImageView];
    
    
    for (int i =0; i<2; i++) {
        _backView = [[UIView alloc]initWithFrame:CGRectMake(DSFixedPX(100), CGRectGetMaxY(_picImageView.frame) + DSFixedPX(50)+ i * DSFixedPX(110), DSDevice_W - DSFixedPX(200), DSFixedPX(90))];
        _backView.backgroundColor = DSColorWithHex(0x3279ca);
       // _backView.layer.borderWidth = 1;
        _backView.layer.masksToBounds = YES;
        _backView.layer.cornerRadius = DSFixedPX(30);
       // _backView.layer.borderColor = [[UIColor whiteColor] CGColor];
        [_loginImageBackView addSubview:_backView];
        
        _phoneLabel = [[UILabel alloc]initWithFrame:CGRectMake(DSFixedPX(30), 0, DSFixedPX(170), DSFixedPX(90))];
        _phoneLabel.textColor = DSColorWithHex(0xffffff);
        _phoneLabel.font = DSFontSize(13);
        if (i == 0) {
            _phoneLabel.text = @"手机号";
        }else if (i == 1){
            _phoneLabel.text = @"项目邀请码";
        }
        [_backView addSubview:_phoneLabel];
        
        _passwordTextFiled = [[UITextField alloc]initWithFrame:CGRectMake(DSFixedPX(210), 0, DSDevice_W - DSFixedPX(350), DSFixedPX(90))];
        _passwordTextFiled.textColor = DSColorWithHex(0xffffff);
        _passwordTextFiled.font = DSFontSize(13);
        if (i == 0) {
           // _passwordTextFiled.placeholder = @"请输入手机号码";
            
            
            _passwordTextFiled.keyboardType = UIKeyboardTypeNumberPad;
            _passwordTextFiled.backgroundColor = DSColorWithHex(0x3279ca);
            _passwordTextFiled.layer.borderColor = [[UIColor clearColor] CGColor];
            _passwordTextFiled.tag = 10000;
            _passwordTextFiled.delegate = self;
            _passwordTextFiled.text = @"18611710683";
            if ([[LPCommonTool readphoneNumber] integerValue] == 0) {
                NSString *holderText = @"请输入手机号码";
                NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
                [placeholder addAttribute:NSForegroundColorAttributeName
                                    value:[UIColor whiteColor]
                                    range:NSMakeRange(0, holderText.length)];
                _passwordTextFiled.attributedPlaceholder = placeholder;
            }else{
                 _passwordTextFiled.text = [NSString stringWithFormat:@"%@",[LPCommonTool readphoneNumber]];
            }
          
        }else if (i == 1){
            _passwordTextFiled.keyboardType =UIKeyboardTypeDefault;
            _passwordTextFiled.secureTextEntry = YES;
            _passwordTextFiled.tag = 10001;
            // panpan?? 测试用
            //18611710683 U9S8
            //_passwordTextFiled.text = @"r0r6";
            if ([[LPCommonTool readPassword]integerValue] == 0) {
                NSString *holderText = @"请输入项目邀请码";
                NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
                [placeholder addAttribute:NSForegroundColorAttributeName
                                    value:[UIColor whiteColor]
                                    range:NSMakeRange(0, holderText.length)];
                _passwordTextFiled.attributedPlaceholder = placeholder;
            }else{
                _passwordTextFiled.text = [NSString stringWithFormat:@"%@",[LPCommonTool readPassword]];
            }
        }
        [_backView addSubview:_passwordTextFiled];
    }
    
    _sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _sureButton.frame = CGRectMake(DSFixedPX(100), CGRectGetMaxY(_backView.frame) + DSFixedPX(40), DSDevice_W - DSFixedPX(200), DSFixedPX(90)) ;
    [_sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [_sureButton setTitleColor:DSColorWithHex(0xffffff) forState:UIControlStateNormal];
    _sureButton.titleLabel.font = DSFontSize(18);
    _sureButton.backgroundColor = [UIColor redColor];
    _sureButton.layer.masksToBounds = YES;
    [_sureButton addTarget:self action:@selector(sureButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    _sureButton.layer.cornerRadius = DSFixedPX(30);
    [_loginImageBackView addSubview:_sureButton];
    
    _conetLabel = [[UILabel alloc]initWithFrame:CGRectMake(DSFixedPX(50), CGRectGetMaxY(_sureButton.frame) +DSFixedPX(50), DSDevice_W - DSFixedPX(100), DSFixedPX(40))];
    _conetLabel.text = @"节目方请登陆PC端";
    _conetLabel.textAlignment = NSTextAlignmentCenter;
    _conetLabel.font = DSFontSize(14);
    _conetLabel.textColor = [UIColor whiteColor];
    [_loginImageBackView addSubview:_conetLabel];
    
    _wwwLabel = [[UILabel alloc]initWithFrame:CGRectMake(DSFixedPX(50), CGRectGetMaxY(_conetLabel.frame)+DSFixedPX(15), DSDevice_W - DSFixedPX(100), DSFixedPX(40))];
    _wwwLabel.text = @"www.muce.tv";
    _wwwLabel.textColor = [UIColor whiteColor];
    _wwwLabel.font = DSFontSize(13);
    _wwwLabel.textAlignment = NSTextAlignmentCenter;
   // [_loginImageBackView addSubview:_wwwLabel];
}
-(void)sureButtonClicked{
    UITextField * numberTextFiled = [self.view viewWithTag:10000];
    UITextField * passWordFiled = [self.view viewWithTag:10001];

    if (numberTextFiled.text.length != 11) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的电话号码" duration:2.0f];
        return;
    }
    if (passWordFiled.text.length <= 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入邀请码" duration:2.0f];
        return;
    }
    
  //  DetailViewController * detailVc = [[DetailViewController alloc]init];
    // detailVc.title = @"项目详情";
//    [self presentViewController:detailVc animated:YES completion:^{
//
//    }];
    
    
    [LPRequestParser postLoginAppUserName:numberTextFiled.text securityCode:passWordFiled.text url:LoginApp block:^(id responseObject, NSError *error) {
        if (responseObject) {
            if ([responseObject[@"status"] isEqualToString:@"success"]) {
                //  1 表示节目方    2 表示样本用户。
                NSString *  identityString =[NSString stringWithFormat:@"%@",responseObject[@"identity"]];
                UITextField * numberTextFiled = [self.view viewWithTag:10000];
                UITextField * passWordFiled = [self.view viewWithTag:10001];
                NSMutableDictionary * userDic = [[NSMutableDictionary alloc]init];
                //预览用户：15110236190     预览码：YL8378
                //样本户：18611710683   R0R6
                
                [userDic setValue:numberTextFiled.text forKey:@"phoneNumber"];
                [userDic setValue:passWordFiled.text forKey:@"password"];
                [userDic setValue:identityString forKey:@"identity"];
                
                [LPCommonTool writeUserDefaultObject:userDic forKey:DSUSERKEY];
                
                DetailViewController * detailVc = [[DetailViewController alloc]init];
                 detailVc.title = @"项目详情";
                [self presentViewController:detailVc animated:YES completion:^{

                }];
            }else{
                [SVProgressHUD showErrorWithStatus:@"登录失败"];
            }
        }else{
            [SVProgressHUD showErrorWithStatus:@"请检测网络哟"];
        }

    }];
}


#pragma mark ***********************************text代理*************************************
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    UITextField *phoneNumberString = [self.view viewWithTag:10000];
    UITextField *passwordNumberString = [self.view viewWithTag:10001];
    //***********内容不能为空判断
    NSString *tem = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]componentsJoinedByString:@""];
    if (![string isEqualToString:tem]) {
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:@"内容不能为空哟" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alertView show];
        return NO;
        
    }
    if (phoneNumberString.text.length > 0 && passwordNumberString.text.length > 3) {
      //  _sureButton.backgroundColor = DSColorWithHex(0x84ccc9);
        _sureButton.userInteractionEnabled = YES;
        
//        if ([string isEqualToString:@""] && textField.text.length == 1) {
//           // _sureButton.backgroundColor =DSColorWithHex(0xcecece);
//            _sureButton.userInteractionEnabled = NO;
//        }
    }else if ([string isEqualToString:@""]){
       // _sureButton.backgroundColor =DSColorWithHex(0xcecece);
      //  _sureButton.userInteractionEnabled = NO;
        
    }
    
    if (range.length == 1 && string.length == 0) {
        return YES;
    }else if ([textField isEqual:phoneNumberString]){
        
        if (textField == phoneNumberString) {
            if ((phoneNumberString.text.length >= 10 && ![string isEqualToString:@""])) {
                return textField.text.length < 11;
            }else{
               // _sureButton.backgroundColor = DSColorWithHex(0xcecece);
               // _sureButton.userInteractionEnabled = NO;
            }
        }    }
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
